package com.hnx.admin.aurora.system.controller;

import com.hnx.admin.aurora.annotation.Log;
import com.hnx.admin.aurora.enums.BusinessType;
import com.hnx.admin.aurora.system.domain.param.dept.query.DeptQuery;
import com.hnx.admin.aurora.system.domain.param.user.form.*;
import com.hnx.admin.aurora.system.domain.param.user.query.UserQuery;
import com.hnx.admin.aurora.system.service.UserService;
import com.hnx.admin.aurora.utils.UUIDUtil;
import com.hnx.admin.aurora.web.json.ResultJson;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/*
创建人： 倔强的头发
创建时间：2025/6/17
功能描述:

*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/system/user")
public class SysUserController {
    private final UserService userService;
    @PreAuthorize("@auth.hasPermi('system:user:list')")
    @GetMapping("/list")
    public ResultJson list(UserQuery userParam){
        return userService.getUserList(userParam);
    }
    /**
     * 获取部门树列表
     */
    @PreAuthorize("@auth.hasPermi('system:user:list')")
    @GetMapping("/deptTree")
    public ResultJson deptTree(DeptQuery dept)
    {
        return userService.deptTree(dept);
    }
    /**
     * 根据用户编号获取详细信息
     */
    @PreAuthorize("@auth.hasPermi('system:user:query')")
    @GetMapping(value = { "/", "/{userId}" })
    public ResultJson getInfo(@PathVariable(value = "userId", required = false) String userId)
    {
        return userService.getInfo(userId);
    }
    /**
     * 修改用户
     */
    @PreAuthorize("@auth.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultJson edit(@Validated @RequestBody UserForm user)
    {
        return userService.updateUser(user);
    }
    /**
     * 状态修改
     */
    @PreAuthorize("@auth.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public ResultJson changeStatus(@RequestBody UserForm user)
    {
        return userService.updateUserStatus(user);
    }
    /**
     * 删除用户
     */
    @PreAuthorize("@auth.hasPermi('system:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userIds}")
    public ResultJson remove(@PathVariable String[] userIds)
    {
        UserDelForm userDelForm = new UserDelForm();
        userDelForm.setUserIds(userIds);
        return userService.deleteUserByIds(userDelForm);
    }
    /**
     * 重置密码
     */
    @PreAuthorize("@auth.hasPermi('system:user:resetPwd')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PutMapping("/resetPwd")
    public ResultJson resetPwd(@RequestBody UserForm user)
    {
        return userService.resetPwd(user);
    }
    /**
     * 根据用户编号获取授权角色
     */
    @PreAuthorize("@auth.hasPermi('system:user:query')")
    @GetMapping("/authRole/{userId}")
    public ResultJson authRole(@PathVariable("userId") String userId)
    {
        return userService.selectRolesByUserId(userId);
    }
    /**
     * 用户授权角色
     */
    @PreAuthorize("@auth.hasPermi('system:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.GRANT)
    @PutMapping("/authRole")
    public ResultJson insertAuthRole(@Valid UserRoleForm user)
    {
        return userService.insertUserAuth(user);
    }
    /**
     * 新增用户
     */
    @PreAuthorize("@auth.hasPermi('system:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultJson add(@Validated @RequestBody UserForm user)
    {
        user.setUserId(UUIDUtil.getUUID());
        return userService.insertUser(user);
    }
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@auth.hasPermi('system:user:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        userService.export(response);
    }
}
